package com.ruoyi.project.task.postpone.controller;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.person.info.domain.PersonInfo;
import com.ruoyi.project.task.mission.domain.TaskMission;
import com.ruoyi.project.task.mission.domain.TaskMissionVo;
import com.ruoyi.project.task.mission.service.ITaskMissionService;
import com.ruoyi.project.task.postpone.domain.TaskPostpone;
import com.ruoyi.project.task.postpone.service.ITaskPostponeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Pathosis
 * @date 2020-09-18 15:22
 * @description:
 */
@Controller
@RequestMapping("/task/postpone")
public class TaskPostponeController extends BaseController{

    @Autowired
    private ITaskPostponeService itps;

    @Autowired
    private ITaskMissionService itms;

    private String prefix = "task/postpone";

    @RequiresPermissions("task:postpone:view")
    @GetMapping
    public String toPostpone(){
        return prefix + "/postpone";
    }

    @RequiresPermissions("task:postpone:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TaskPostpone taskPostpone){
        startPage();

        List<TaskPostpone> list = itps.selectAllTaskPostpone(taskPostpone);

        return getDataTable(list);
    }

    @GetMapping("/add")
    public String toAdd(ModelMap mmap){
        TaskMissionVo tmv = new TaskMissionVo();
        //查询所有的延期任务
        List<TaskMissionVo> missionList = itms.selectTaskMissionList(tmv);
        mmap.addAttribute("missionList", missionList);
         return prefix + "/add";
    }

    @RequiresPermissions("task:postpone:add")
    @Log(title = "任务延期", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated TaskPostpone taskPostpone)
    {
        return toAjax(itps.insertTaskPostpone(taskPostpone));
    }


    @RequiresPermissions("task:postpone:remove")
    @Log(title = "任务延期删除删除", businessType = BusinessType.INSERT)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult removeSave(String ids)
    {
        try
        {
            return toAjax(itps.deleteTaskPostponeByIds(ids));
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }

    @GetMapping("/edit/{postponeId}")
    public String edit(@PathVariable("postponeId") Long postponeId, ModelMap mmap)
    {
        mmap.addAttribute("taskPostpone", itps.selectTaskPostponeById(postponeId));
        return prefix + "/edit";
    }

    @RequiresPermissions("task:postpone:edit")
    @Log(title = "任务延期管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated TaskPostpone taskPostpone) {
        return toAjax(itps.updateTaskPostpone(taskPostpone));
    }
}
